#pragma once

#include "JoinNode.h"

#include "ValueSpecification.h"

namespace uml {

const bool JoinNode::isCombineDuplicate_default_ = true;
JoinNode::JoinNode(const QString& umlType)
    : ControlNode(umlType) {}

void JoinNode::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    ControlNode::fromJson(json, errorList);
    isCombineDuplicate_ = json.value("isCombineDuplicate").toBool(isCombineDuplicate_default_);
    joinSpec_ = JsonSerializable::create<ValueSpecification>(json.value("joinSpec"), errorList);

}

void JoinNode::toJson(QJsonObject& json) {
    ControlNode::toJson(json);
    if (isCombineDuplicate_ != isCombineDuplicate_default_) {
        json.insert("isCombineDuplicate", isCombineDuplicate_);
    }
    if (joinSpec_) {
        QJsonObject joinSpec_json;
        joinSpec_->toJson(joinSpec_json);
        json.insert("joinSpec", joinSpec_json);
    }
}

/// <summary>
/// isCombineDuplicate getter
/// Indicates whether incoming tokens having objects with the same identity are
/// combined into one by the JoinNode.
/// </summary>
bool JoinNode::isCombineDuplicate() const {
    return isCombineDuplicate_;
}

/// <summary>
/// isCombineDuplicate setter
/// Indicates whether incoming tokens having objects with the same identity are
/// combined into one by the JoinNode.
/// </summary>
void JoinNode::setIsCombineDuplicate(bool value) {
    isCombineDuplicate_ = value;
}

/// <summary>
/// joinSpec getter
/// A ValueSpecification giving the condition under which the JoinNode will offer a
/// token on its outgoing ActivityEdge. If no joinSpec is specified, then the
/// JoinNode will offer an outgoing token if tokens are offered on all of its
/// incoming ActivityEdges (an "and" condition).
/// </summary>
shared_ptr<ValueSpecification> JoinNode::joinSpec() const {
    return joinSpec_;
}

/// <summary>
/// joinSpec setter
/// A ValueSpecification giving the condition under which the JoinNode will offer a
/// token on its outgoing ActivityEdge. If no joinSpec is specified, then the
/// JoinNode will offer an outgoing token if tokens are offered on all of its
/// incoming ActivityEdges (an "and" condition).
/// </summary>
void JoinNode::setJoinSpec(shared_ptr<ValueSpecification> value) {
    joinSpec_ = value;
}

} // namespace uml
